package com.ykdl.member.kid.adapters;

import java.util.ArrayList;
import java.util.List;

import com.lidroid.xutils.BitmapUtils;
import com.ykdl.member.kid.R;
import com.ykdl.member.kid.models.TopicBean;
import com.ykdl.member.kid.widget.AsyncImageView;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.RelativeLayout.LayoutParams;

public class HeatImageListAdpater extends BaseAdapter {
	private List<TopicBean> topics;
	private Context activity;
	private BitmapUtils bitmapUtil;
	private int gridview_item_image_height;

	public HeatImageListAdpater(Context activity, BitmapUtils bitmapUtil,
			int gridview_item_image_height) {
		this.activity = activity;
		this.bitmapUtil = bitmapUtil;
		this.gridview_item_image_height = gridview_item_image_height;

	}

	public void setList(List<TopicBean> topics) {
		if (this.topics == null) {
			this.topics = new ArrayList<TopicBean>();
		}
		this.topics.addAll(topics);

	}

	@Override
	public int getCount() {
		return topics != null && topics.size() > 0 ? topics.size() : 0;
	}

	@Override
	public Object getItem(int position) {
		return topics != null && topics.size() > 0 ? topics.get(position)
				: null;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHold hold = null;
		if (convertView == null) {
			hold = new ViewHold();
			convertView = LayoutInflater.from(activity).inflate(
					R.layout.heat_image_list_item_view, null);
			hold.iv_heat_image = (AsyncImageView) convertView
					.findViewById(R.id.iv_heat_image);

			LayoutParams iv_heat_image_params = (LayoutParams) hold.iv_heat_image
					.getLayoutParams();
			iv_heat_image_params.width = LayoutParams.FILL_PARENT;
			iv_heat_image_params.height = gridview_item_image_height;
			hold.iv_heat_image.setLayoutParams(iv_heat_image_params);
			hold.tv_heat_image_title = (TextView) convertView
					.findViewById(R.id.tv_heat_image_title);

			convertView.setTag(hold);
		} else {
			hold = (ViewHold) convertView.getTag();
		}

		if (topics == null)
			return null;
		final TopicBean topic = topics.get(position);
		bindData(hold, topic);
		return convertView;
	}

	class ViewHold {
		AsyncImageView iv_heat_image;
		TextView tv_heat_image_title;
	}

	private void bindData(final ViewHold hold, final TopicBean topic) {
		final String heat_iamge_url = getImageUrl(topic);
		if (heat_iamge_url != null) {
			hold.iv_heat_image.setImageDrawable(null);
			bitmapUtil
					.configDefaultLoadingImage(R.drawable.iask_main_middle_img);
			bitmapUtil.display(hold.iv_heat_image, heat_iamge_url);
		} else {
			hold.iv_heat_image.setImageDrawable(null);
			hold.iv_heat_image
					.setImageResource(R.drawable.iask_main_middle_img);
		}
		if (topic.getPost_actor() != null) {
			if (topic.getPost_actor().getDisplay_name() != null
					&& !topic.getPost_actor().getDisplay_name().equals("")) {
				if (topic.getPost_actor().getDisplay_name().length() > 5) {
					hold.tv_heat_image_title.setText(topic.getPost_actor()
							.getDisplay_name().substring(0, 4)
							+ "...");
				} else {
					hold.tv_heat_image_title.setText(topic.getPost_actor()
							.getDisplay_name());
				}
			} else {
				hold.tv_heat_image_title.setText("");
			}

		} else {
			hold.tv_heat_image_title.setText("");
		}

	}

	private String getImageUrl(final TopicBean topic) {
		if (topic.getFile_metas() != null) {
			final String image_url = topic.getFile_metas().get(0)
					.getDownload_urls().getBig().getUrl();
			return image_url;
		}
		return null;
	}

}
